/**
 *
 */
package hichecker.ui.custom_views.passing;

import hichecker.controller.Config;
import hichecker.university.UniversityPart;

import java.util.ArrayList;
import java.util.List;


/**
 *
 */
public abstract class PassingCondition {

	protected List<PassingCondition> conditions;
	protected UniversityPart object;
	protected ConditionType type;
	protected Object value;

	public enum ConditionType {SUBS_PASSED (Config.CONDITION_SUBS_PASSED),
							   SUM_POINTS_GREATER_EQUAL (Config.CONDITION_SUM_POINTS_GREATER_EQUAL),
							   GRADE_GREATER_EQUAL (Config.CONDITION_GRADE_GREATER_EQUAL);

		private String name;

		private ConditionType(String name) {
			this.name = name;
		}

		@Override
		public String toString() {
			return name;
		}

	};

	public PassingCondition() {
	}

	public PassingCondition(UniversityPart object, ConditionType type, Object value) {
		this.object = object;
		this.type = type;
		this.value = value;
	}

	public void addCondition(PassingCondition condition) {
		getConditions().add(condition);
	}

	public void removeCondition(PassingCondition condition) {
		getConditions().remove(condition);
	}

	protected List<PassingCondition> getConditions() {
		if (conditions == null)
			conditions = new ArrayList<PassingCondition>();
		return conditions;
	}

	public UniversityPart getObject() {
		return object;
	}

	public ConditionType getType() {
		return type;
	}

	public Object getValue() {
		return value;
	}

	public abstract boolean validate();

	public abstract String getName();

	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof PassingCondition))
			return false;

		PassingCondition condition = (PassingCondition) obj;
		UniversityPart object = condition.getObject();
		ConditionType type = condition.getType();
		Object value = condition.getValue();

		boolean result = true;

		result &= object == null ? this.object == null : object.equals(this.object);
		result &= type == null ? this.type == null : type.equals(this.type);
		result &= value == null ? this.value == null : value.equals(this.value);

		return result;
	}
}
